package com.zrspring.libv2.util;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Version: 1.0
 * Author: rui
 * CreatDate: 2016年08月10日下午7:53 53
 * Encoding:UTF-8
 * Description:
 */
public class AmountUtil {

    public static String getAmoutTextFromDoubleString(String doubleStr) {
        DecimalFormat df = new DecimalFormat("######0.00");
        double amount;
        try {
            amount = Double.valueOf(doubleStr);
        } catch (Exception e) {
            amount = 0d;
        }
        return df.format(amount);
    }

    public static String getAmountTextFromDouble(double amount) {
        return new DecimalFormat("######0.00").format(amount);
    }

    public static String getTotalAmount(String doubleOne, String doubleTwo) {
        DecimalFormat df = new DecimalFormat("######0.00");
        double result = 0d;
        double amountOne;
        double amountTwo;
        try {
            amountOne = Double.valueOf(doubleOne);
        } catch (Exception e) {
            amountOne = 0d;
        }
        try {
            amountTwo = Double.valueOf(doubleTwo);
        } catch (Exception e) {
            amountTwo = 0d;
        }
        result = add(amountOne, amountTwo);
        return df.format(result);
    }

    public static String getTotalAmountThree(String doubleOne, String doubleTwo, String doubleThree) {
        DecimalFormat df = new DecimalFormat("######0.00");
        if (TextUtils.isEmpty(doubleOne)) {
            doubleOne = "0.00";
        }
        if (TextUtils.isEmpty(doubleTwo)) {
            doubleOne = "0.00";
        }
        if (TextUtils.isEmpty(doubleThree)) {
            doubleOne = "0.00";
        }
        double result = 0d;
        double amountOne;
        double amountTwo;
        double amountThree;
        try {
            amountOne = Double.valueOf(doubleOne);
        } catch (Exception e) {
            amountOne = 0d;
        }
        try {
            amountTwo = Double.valueOf(doubleTwo);
        } catch (Exception e) {
            amountTwo = 0d;
        }
        try {
            amountThree = Double.valueOf(doubleThree);
        } catch (Exception e) {
            amountThree = 0d;
        }
        result = add(amountOne, amountTwo);
        result = add(result, amountThree);
        return df.format(result);
    }

    /**
     * @MethodthName: getPriceFromInt
     * @Description: 获取一个价格数据 ￥50.00
     * @params: [price]
     * @Return:java.lang.String
     * @Throw:
     */
    public static String getYuanString(String price) {
        StringBuffer b = new StringBuffer();
        b.append("￥");
        b.append(new DecimalFormat("0.00").format(Double.parseDouble(price)));
        return b.toString();
    }

    /**
     * 提供精确加法计算的add方法
     *
     * @param value1 被加数
     * @param value2 加数
     * @return 两个参数的和
     */
    public static double add(double value1, double value2) {
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.add(b2).doubleValue();
    }

    /**
     * 提供精确减法运算的sub方法
     *
     * @param value1 被减数
     * @param value2 减数
     * @return 两个参数的差
     */
    public static double sub(double value1, double value2) {
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.subtract(b2).doubleValue();
    }

    /**
     * 提供精确乘法运算的mul方法
     *
     * @param value1 被乘数
     * @param value2 乘数
     * @return 两个参数的积
     */
    public static double mul(double value1, double value2) {
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.multiply(b2).doubleValue();
    }

    /**
     * 提供精确的除法运算方法div
     *
     * @param value1 被除数
     * @param value2 除数
     * @param scale  精确范围
     * @return 两个参数的商
     * @throws IllegalAccessException
     */
    public static double div(double value1, double value2, int scale) throws IllegalAccessException {
        //如果精确范围小于0，抛出异常信息
        if (scale < 0) {
            throw new IllegalAccessException("precision is less than 0");
        }
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.divide(b2, scale).doubleValue();
    }

}
